package com.dongdong;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;

import java.io.FileInputStream;
import java.io.FileReader;
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.Security;
import java.security.Signature;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Base64;

/**
 * @author dongdong
 * @date 2024/7/8 11:18:04
 */
public class LicenseGeneratorCode {
    public static void main(String[] args) throws Exception {

        CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
        X509Certificate cert = (X509Certificate) certificateFactory.generateCertificate(new FileInputStream("ca.crt"));

        // 自己修改 license内容
        // 注意licenseId要一致
        // 里面的内容是激活webstorm
        String licensePart = "{\n" +
                "    \"licenseId\": \"dongdong\",\n" +
                "    \"licenseeName\": \"dongdong\",\n" +
                "    \"assigneeName\": \"dongdong\",\n" +
                "    \"assigneeEmail\": \"null\",\n" +
                "    \"licenseRestriction\": \"null\",\n" +
                "    \"checkConcurrentUse\": false,\n" +
                "    \"products\": [\n" +
                "      {\n" +
                "        \"code\": \"PFASTREQUEST\",\n" +
                "        \"fallbackDate\": \"2099-02-09\",\n" +
                "        \"paidUpTo\": \"2099-02-09\",\n" +
                "        \"extended\": true\n" +
                "      }\n" +
                "    ],\n" +
                "    \"metadata\": \"0120230914PSAX000005\",\n" +
                "    \"hash\": \"TRIAL:-1920204289\",\n" +
                "    \"gracePeriodDays\": 7,\n" +
                "    \"isAutoProlongated\": true\n" +
                "  }";
        String licenseId = "dongdong";
        byte[] licensePartBytes = licensePart.getBytes(StandardCharsets.UTF_8);
        String licensePartBase64 = Base64.getEncoder().encodeToString(licensePartBytes);

        PrivateKey privateKey = getPrivateKey();


        Signature signature = Signature.getInstance("SHA1withRSA");
        signature.initSign(privateKey);
        signature.update(licensePartBytes);
        byte[] signatureBytes = signature.sign();

        String sigResultsBase64 = Base64.getEncoder().encodeToString(signatureBytes);
//        verify
//        Signature verifySignature = Signature.getInstance("SHA1withRSA");
//        verifySignature.initVerify(cert.getPublicKey());
//        verifySignature.update(Base64.getDecoder().decode(licensePartBase64));
//        boolean verify = verifySignature.verify(Base64.getDecoder().decode(sigResultsBase64));
//        System.out.println(verify);


        // Combine results as needed
        String result = licenseId + "-" + licensePartBase64 + "-" + sigResultsBase64 + "-" + Base64.getEncoder().encodeToString(cert.getEncoded());
        System.out.println(result);

    }


    static PrivateKey getPrivateKey() throws Exception {
        Security.addProvider(new BouncyCastleProvider());
        PEMParser pemParser = new PEMParser(new FileReader("ca.key"));
        JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
        Object object = pemParser.readObject();
        KeyPair kp = converter.getKeyPair((PEMKeyPair) object);
        return kp.getPrivate();
    }
}
